import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
import 'package:get/get.dart';

import '../base/constant.dart';
import '../base/eventbus.dart';
import '../base/log.dart';
import '../util/physiological_cycle_count.dart';
import '../util/string_util.dart';

class Native {
  ///这个定义健康数据的来源
  ///1. 表示JCRing 2. 表示JCVital
  static const HealthDataApp = 1;

  static const MethodChannel _callChannel = MethodChannel('jcring_call');
  static const MethodChannel _resultChannel = MethodChannel('jcring_result');

  /*
  * 设置数据返回监听
  * */
  static void initMethodResultBack() {
    DartLog.i("Native", "initMethodResultBack 初始化方法回调");
    _resultChannel.setMethodCallHandler((call) async {
      DartLog.i("Native",
          "initMethodResultBack 收到原生APP的数据返回 method: ${call.method} arguments:${call.arguments}");
      EventBusUtil.getInstance().fire(NativeEvent(call.method, call.arguments));
    });
  }

  //返回到原生APP界面
  static void backPressed() {
    if (Native.HealthDataApp == 1) {
      //返回原生按钮
      _callChannel.invokeMethod('back_pressed');
    } else if (Native.HealthDataApp == 2) {
      Get.back();
    }
  }

  //获取APP的userID
  static void readUserId() {
    if (HealthDataApp == 1) {
      _callChannel.invokeMethod(HealthNativeDataType.READ_USER_ID);
    } else if (HealthDataApp == 2) {
      EventBusUtil.getInstance()
          .fire(FlutterCallEvent(HealthNativeDataType.READ_USER_ID, null));
    }
  }

  //初始化女性健康数据
  static void initWomenHealthData() {
    if (HealthDataApp == 1) {
      _callChannel.invokeMethod(HealthNativeDataType.INIT_WOMEN_HEALTH_DATA);
    } else if (HealthDataApp == 2) {
      EventBusUtil.getInstance()
          .fire(FlutterCallEvent(HealthNativeDataType.INIT_WOMEN_HEALTH_DATA, null));
    }
  }

  //已设置女性健康数据
  static void setWomenHealthData(Map<String, dynamic> mapValue) {
    if (HealthDataApp == 1) {
      _callChannel.invokeMethod(HealthNativeDataType.SET_WOMEN_HEALTH_DATA, mapValue);
    } else if (HealthDataApp == 2) {
      EventBusUtil.getInstance()
          .fire(FlutterCallEvent(HealthNativeDataType.SET_WOMEN_HEALTH_DATA, mapValue));
    }
  }

  //女性健康周期历史
  static void readHealthHistoryList() {
    if (HealthDataApp == 1) {
      _callChannel.invokeMethod(HealthNativeDataType.READ_HEALTH_HISTORY);
    } else if (HealthDataApp == 2) {
      EventBusUtil.getInstance()
          .fire(FlutterCallEvent(HealthNativeDataType.READ_HEALTH_HISTORY, null));
    }
  }

  static void writeHealthHistoryList(String value) {
    if (HealthDataApp == 1) {
      _callChannel.invokeMethod(HealthNativeDataType.WRITE_HEALTH_HISTORY, value);
    } else if (HealthDataApp == 2) {
      EventBusUtil.getInstance()
          .fire(FlutterCallEvent(HealthNativeDataType.WRITE_HEALTH_HISTORY, value));
    }
  }

  //女性健康读取一个月的温度变化
  static void readTemperatureDataList() {
    Map<String, dynamic> mapValue = <String, dynamic>{};
    mapValue['time'] = DateFormat("yyyy.MM.dd").format(DateTime.now());
    mapValue['days'] = 30;
    if (HealthDataApp == 1) {
      _callChannel.invokeMethod(HealthNativeDataType.READ_TEMPERATURE_DATA, mapValue);
    } else if (HealthDataApp == 2) {
      EventBusUtil.getInstance()
          .fire(FlutterCallEvent(HealthNativeDataType.READ_TEMPERATURE_DATA, mapValue));
    }
  }

  //女性健康读取心率变异性数据
  static void readHeartRateVariabilityData() {
    Map<String, dynamic> mapValue = <String, dynamic>{};
    mapValue['time'] = DateFormat("yyyy.MM.dd").format(DateTime.now());
    mapValue['days'] = 30;
    if (HealthDataApp == 1) {
      _callChannel.invokeMethod(HealthNativeDataType.READ_HRV_DATA, mapValue);
    } else if (HealthDataApp == 2) {
      EventBusUtil.getInstance()
          .fire(FlutterCallEvent(HealthNativeDataType.READ_HRV_DATA, mapValue));
    }
  }

  //女性健康读取静息心率数据
  static void readHeartRateData() {
    Map<String, dynamic> mapValue = <String, dynamic>{};
    mapValue['time'] = DateFormat("yyyy.MM.dd").format(DateTime.now());
    mapValue['days'] = 30;
    if (HealthDataApp == 1) {
      _callChannel.invokeMethod(HealthNativeDataType.READ_HR_DATA, mapValue);
    } else if (HealthDataApp == 2) {
      EventBusUtil.getInstance()
          .fire(FlutterCallEvent(HealthNativeDataType.READ_HR_DATA, mapValue));
    }
  }

  ///写入女性健康数据
  static void writeHealthItemData(String todayHealthTitle,String todayHealthContent) {
    if (HealthDataApp == 1) {
      Map<String, dynamic> mapValue = <String, dynamic>{};
      mapValue['item_content'] = todayHealthTitle;
      mapValue['item_remark'] = todayHealthContent;
      _callChannel.invokeMethod("women_health_item_data", mapValue);
    }
  }

  ///设置受孕周期功能
  static void setWomenConceptionData() {
    if (HealthDataApp == 1) {
      _callChannel.invokeMethod(HealthNativeDataType.SET_WOMEN_CONCEPTION_DATA);
    } else if (HealthDataApp == 2) {
      EventBusUtil.getInstance()
          .fire(FlutterCallEvent(HealthNativeDataType.SET_WOMEN_CONCEPTION_DATA,null));
    }
  }

  ///重置受孕周期功能
  static void resetWomenConceptionData() {
    if (HealthDataApp == 1) {
      _callChannel.invokeMethod(HealthNativeDataType.RESET_WOMEN_CONCEPTION_DATA);
    } else if (HealthDataApp == 2) {
      EventBusUtil.getInstance()
          .fire(FlutterCallEvent(HealthNativeDataType.RESET_WOMEN_CONCEPTION_DATA,null));
    }
  }

  ///设置月经周期功能
  static void setWomenPredictionData() {
    if (HealthDataApp == 1) {
      _callChannel.invokeMethod(HealthNativeDataType.SET_WOMEN_PREDICTION_DATA);
    } else if (HealthDataApp == 2) {
      EventBusUtil.getInstance()
          .fire(FlutterCallEvent(HealthNativeDataType.SET_WOMEN_PREDICTION_DATA,null));
    }
  }

  //初始化APP名称
  static String initAppName() {
    if (Native.HealthDataApp == 2) {
      return "JCVital";
    }
    return "jcring";
  }

}
